﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using YunShop_WebAPI.Model;
using System.Collections;
using System.Data;
using System.IO;
using System.Configuration;
using YunShop_WebAPI.BLL;
using YunShop_WebAPI.BLL.SxfPay;

namespace YunShop_WebAPI.Controllers
{
    public class CloudPosController : Controller
    {
        ///string connstr = SystemInfo.GetClusterConn(mCorpID);
        /// <summary>
        /// 安卓收银端接口
        /// </summary>
        /// <returns></returns>

        public ActionResult Index()
        {
            return View();
        }

        private JsonResult GetBaseInfoResult(bool bOk, string outData, CountPosDataModel count, bool bAllowGet = false)
        {
            var data = Common.ConvertToBase64String(outData);
            //var data = outData;
            object obj = null;
            if (count != null)
                obj = new
                {
                    msg = bOk ? "success" : "fail",
                    frowcount = count.frowcount,
                    fpagesize = count.fpagesize,
                    fpagecount = count.fpagecount,
                    fpageindex = count.fpageindex,
                    flasttime = count.flasttime,
                    data
                };
            else
                obj = new
                {
                    msg = bOk ? "success" : "fail",
                    frowcount = 0,
                    fpagesize = 0,
                    fpagecount = 0,
                    fpageindex = 0,
                    flasttime = 0,
                    data
                };
            return new JsonResult
            {
                Data = obj,
                JsonRequestBehavior = bAllowGet ? JsonRequestBehavior.AllowGet : JsonRequestBehavior.DenyGet
            };
        }

        private JsonResult GetPosBillResult(bool bState, string outData, bool bAllowGet = false)
        {
            var data = Common.ConvertToBase64String(Common.GetValidJson(outData));
            object obj = new
            {
                state = bState ? "success" : "fail",
                data
            };
            return new JsonResult
            {
                Data = obj,
                JsonRequestBehavior = bAllowGet ? JsonRequestBehavior.AllowGet : JsonRequestBehavior.DenyGet
            };
        }
               
        private JsonResult GetJsonResult(bool bState, string outData, bool bAllowGet = false)
        {
            var data = Common.ConvertToBase64String(outData);
            object obj = new
            {
                state = bState ? "success" : "fail",
                data
            };
            return new JsonResult
            {
                Data = obj,
                JsonRequestBehavior = bAllowGet ? JsonRequestBehavior.AllowGet : JsonRequestBehavior.DenyGet
            };
        }

        /// <summary>
        /// 操作员登录
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public JsonResult UserLogin(UserLoginRequest request)
        {
            bool bOk = false;
            string data = "";
            CountPosDataModel count = null;
            try
            {
                CloudPosService mAppPosSvr = new CloudPosService();
                ArrayList queryData = mAppPosSvr.UserLogin(request);
                if (queryData != null && queryData.Count > 0)
                {
                    data = SerializerHelper.SerializeToString(queryData);
                    count = new CountPosDataModel() { frowcount = queryData.Count };
                }
                bOk = true;
            }
            catch (Exception err)
            {
                data = err.Message;
            }
            return GetBaseInfoResult(bOk, data, count);
        }  

        /// <summary>
        /// 商品表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public JsonResult GetPosBaseInfo(PrPosDataRequest request)
        {
            //{"mItem_id":"","mkey":"","mtype":4,"mHasUnit":"N","mBranch_no":"1001","mUserNumber":"8888","mCode":"","mCategory":"","PageIndex":"1"}
            bool bOk = false;
            string data = "";
            CountPosDataModel count = new CountPosDataModel();
            try
            {
                CloudPosService mDataSvr = new CloudPosService();
                ArrayList queryData = mDataSvr.GetPosBaseInfo(request, ref count);
                if (queryData != null)
                {
                    data = SerializerHelper.SerializeToString(queryData);
                }
                bOk = true;
            }
            catch (Exception err)
            {
                //WriteLog("PosItem", err.Message.ToString(), request);
                data = err.Message;
            }
            return GetBaseInfoResult(bOk, data, count, true);
        }

        /// <summary>
        /// 零售单据保存
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public JsonResult SetPosBillData(PrPosDataRequest request)
        {            
            bool bOk = false;
            string json = "";
            CloudPosDataModel mResult = new CloudPosDataModel();
            try
            {
                CloudPosService mDataSvr = new CloudPosService();                
                bOk = mDataSvr.SetPosBillData(request, ref mResult);

                if (bOk)
                {
                    Hashtable result = new Hashtable();
                    result["fbillid"] = mResult.FBillID;
                    result["fmessage"] = mResult.FMessage;
                    result["fcbillid"] = mResult.FCBillID;
                    result["fclassid"] = mResult.FClassID;
                    result["fmemberid"] = mResult.FMemberID;
                    result["fmemberamt"] = mResult.FMemberAmt;
                    result["fmemberpoint"] = mResult.FMemberPoint;
                    json = PluSoft.Utils.JSON.Encode(result);
                }
                else {
                    json = mResult.FMessage;
                }
            }
            catch (Exception err)
            {
                mResult.FMessage = err.Message;
            }
            return GetPosBillResult(bOk, json, true);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public JsonResult GetPosClass(PrPosDataRequest request)
        {
            bool bOk = false;
            string json = "";
            PosClassModel mResult = new PosClassModel();
            mResult.KInterID = 0;
            mResult.FBillNo = "";
            try
            {
                CloudPosService mDataSvr = new CloudPosService();
                Int64 mBillID = mDataSvr.GetPosClass(request, ref mResult);

                Hashtable result = new Hashtable();
                result["fbillid"] = mResult.KInterID;
                result["fbillno"] = mResult.FBillNo;
                json = PluSoft.Utils.JSON.Encode(result);

                if (mBillID > 0) bOk = true;
            }
            catch (Exception err)
            {
                //mResult.FMessage = err.Message;
            }
            return GetPosBillResult(bOk, json, true);
        }


        /// <summary>
        /// 交班
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public JsonResult SetPosClass(PrPosDataRequest request)
        {
            bool bOk = false;
            string json = "";
            PosClassModel mResult = new PosClassModel();
            mResult.KInterID = 0;
            mResult.FBillNo = "";
            try
            {
                CloudPosService mDataSvr = new CloudPosService();
                Int64 mBillID = mDataSvr.SetPosClass(request, ref mResult);

                Hashtable result = new Hashtable();
                result["fbillid"] = mResult.KInterID;
                result["fbillno"] = mResult.FBillNo;
                json = PluSoft.Utils.JSON.Encode(result);

                if (mBillID > 0) bOk = true;
            }
            catch (Exception err)
            {
                //mResult.FMessage = err.Message;
            }
            return GetPosBillResult(bOk, json, true);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public JsonResult GetBillOrderNo(PrPosDataRequest request)
        {
            bool bOk = false;
            string json = "";
            BillOrderModel mResult = new BillOrderModel();
            mResult.KInterID = 0;
            mResult.FBillNo = "";
            try
            {
                CloudPosService mDataSvr = new CloudPosService();
                Int64 mBillID = mDataSvr.GetBillOrderNo(request, ref mResult);

                Hashtable result = new Hashtable();
                result["fbillid"] = mResult.KInterID;
                result["fbillno"] = mResult.FBillNo;
                json = PluSoft.Utils.JSON.Encode(result);

                if (mBillID > 0) bOk = true;
            }
            catch (Exception err)
            {
                //mResult.FMessage = err.Message;
            }
            return GetPosBillResult(bOk, json, true);
        }

        /// <summary>
        /// 会员充值
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public JsonResult SetMemberDeposit(PrPosDataRequest request)
        {
            bool bOk = false;
            string json = "";
            MemberResultModel mResult = new MemberResultModel();
            try
            {
                CloudPosService mDataSvr = new CloudPosService();
                bOk = mDataSvr.SetMemberDeposit(request, ref mResult);

                if (bOk)
                {
                    Hashtable result = new Hashtable();
                    result["fmemberid"] = mResult.FMemberID;
                    result["famount"] = mResult.FAmount;
                    result["fpoint"] = mResult.FPoint;
                    result["fqty"] = mResult.FQty;
                    result["fstockqty"] = mResult.FStockQty;
                    result["fmessage"] = mResult.FMessage;
                    json = PluSoft.Utils.JSON.Encode(result);
                }
                else {
                    json = mResult.FMessage;
                }
            }
            catch (Exception err)
            {
                mResult.FMessage = err.Message;
            }
            return GetPosBillResult(bOk, json, true);
        }

        /// <summary>
        /// 会员充值退款
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public JsonResult SetMemberRefund(PrPosDataRequest request)
        {
            bool bOk = false;
            string json = "";
            MemberResultModel mResult = new MemberResultModel();
            try
            {
                CloudPosService mDataSvr = new CloudPosService();
                bOk = mDataSvr.SetMemberRefund(request, ref mResult);

                if (bOk)
                {
                    Hashtable result = new Hashtable();
                    result["fmemberid"] = mResult.FMemberID;
                    result["famount"] = mResult.FAmount;
                    result["fpoint"] = mResult.FPoint;
                    result["fqty"] = mResult.FQty;
                    result["fstockqty"] = mResult.FStockQty;
                    result["fmessage"] = mResult.FMessage;
                    json = PluSoft.Utils.JSON.Encode(result);
                }
                else
                {
                    json = mResult.FMessage;
                }
            }
            catch (Exception err)
            {
                mResult.FMessage = err.Message;
            }
            return GetPosBillResult(bOk, json, true);
        }

        /// <summary>
        /// 数据查询
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public JsonResult GetPosDataList(PrPosDataRequest request)
        {
            bool bOk = false;
            string json = "";
            MemberResultModel mResult = new MemberResultModel();
            try
            {
                CloudPosService mDataSvr = new CloudPosService();
                ArrayList mdata = mDataSvr.GetPosDataList(request, ref bOk);

                if (bOk)
                {
                    json = PluSoft.Utils.JSON.Encode(mdata);
                }
                else {
                    json = "没有查询到相关数据.";
                }

            }
            catch (Exception err)
            {
                mResult.FMessage = err.Message;
            }
            return GetPosBillResult(bOk, json, true);
        }

        /// <summary>
        /// 交班清单
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public JsonResult GetPosClassList(PrPosDataRequest request)
        {
            bool bOk = false;
            string json = "";
            MemberResultModel mResult = new MemberResultModel();
            try
            {
                CloudPosService mDataSvr = new CloudPosService();
                ArrayList mdata = mDataSvr.GetPosClassList(request, ref bOk);

                if (bOk)
                {
                    json = PluSoft.Utils.JSON.Encode(mdata);
                }
                else
                {
                    json = "没有查询到相关数据.";
                }
            }
            catch (Exception err)
            {
                mResult.FMessage = err.Message;
            }
            return GetPosBillResult(bOk, json, true);
        }

        /// <summary>
        /// 获取后台地址
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public JsonResult GetMainWebUrl(PrPosDataRequest request)
        {
            bool bOk = false;
            string json = "";
            try
            {
                CloudPosService mDataSvr = new CloudPosService();
                json = mDataSvr.GetMainWebUrl(request, ref bOk);
            }
            catch (Exception err)
            {
                //mResult.FMessage = err.Message;
            }
            return GetJsonResult(bOk, json, true);
        }

        /// <summary>
        /// 会员资料修改保存
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public JsonResult SetMemberInfo(PrPosDataRequest request)
        {
            bool bOk = false;
            string json = "";
            string msg = "没有查询到相关数据";
            MemberResultModel mResult = new MemberResultModel();
            try
            {
                CloudPosService mDataSvr = new CloudPosService();
                ArrayList mdata = mDataSvr.SetMemberInfo(request, ref bOk, ref msg);

                if (bOk)
                {
                    json = PluSoft.Utils.JSON.Encode(mdata);
                }
                else
                {
                    json = msg;
                }

            }
            catch (Exception err)
            {
                mResult.FMessage = err.Message;
            }
            return GetPosBillResult(bOk, json, true);            
        }


        /// <summary>
        /// 会员读卡
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public JsonResult GetMemberInfo(PrPosDataRequest request)
        {
            bool bOk = false;
            string json = "";
            MemberResultModel mResult = new MemberResultModel();
            try
            {
                CloudPosService mDataSvr = new CloudPosService();
                ArrayList mdata = mDataSvr.GetMemberInfo_Api(request, ref bOk);

                if (bOk)
                {
                    json = PluSoft.Utils.JSON.Encode(mdata);
                }
                else
                {
                    json = "没有查询到相关数据.";
                }

            }
            catch (Exception err)
            {
                mResult.FMessage = err.Message;
            }
            return GetPosBillResult(bOk, json, true);
        }

        /// <summary>
        /// 商品库
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public JsonResult GetSysGoodsInfo(PrPosDataRequest request)
        {
            bool bOk = false;
            string json = "";
            MemberResultModel mResult = new MemberResultModel();
            try
            {
                CloudPosService mDataSvr = new CloudPosService();
                ArrayList mdata = mDataSvr.GetSysGoodsInfo(request, ref bOk);

                if (bOk)
                {
                    json = PluSoft.Utils.JSON.Encode(mdata);
                }
                else
                {
                    json = "没有查询到相关数据.";
                }

            }
            catch (Exception err)
            {
                mResult.FMessage = err.Message;
            }
            return GetPosBillResult(bOk, json, true);
        }

        /// <summary>
        /// 业务单据保存
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public JsonResult SetBillData(PrPosDataRequest request)
        {
            bool bOk = false;
            string json = "";
            BillOrderModel mResult = new BillOrderModel();
            mResult.KInterID = 0;
            mResult.FBillNo = "";
            try
            {
                CloudPosService mDataSvr = new CloudPosService();
                Int64 mBillID = mDataSvr.SetBillData(request, ref mResult);

                Hashtable result = new Hashtable();
                result["fbillid"] = mResult.KInterID;
                result["fbillno"] = mResult.FBillNo;
                json = PluSoft.Utils.JSON.Encode(result);

                if (mBillID > 0) bOk = true;
            }
            catch (Exception err)
            {
                //mResult.FMessage = err.Message;
            }
            return GetPosBillResult(bOk, json, true);
        }

        /// <summary>
        /// 业务单据保存
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public JsonResult SetBillCheck(PrPosDataRequest request)
        {
            bool bOk = false;
            string json = "";
            BillOrderModel mResult = new BillOrderModel();
            mResult.KInterID = 0;
            mResult.FBillNo = "";
            try
            {
                CloudPosService mDataSvr = new CloudPosService();
                Int64 mBillID = mDataSvr.SetBillCheck(request, ref mResult);

                Hashtable result = new Hashtable();
                result["fbillid"] = mResult.KInterID;
                result["fbillno"] = mResult.FBillNo;
                json = PluSoft.Utils.JSON.Encode(result);

                if (mBillID > 0) bOk = true;
            }
            catch (Exception err)
            {
                //mResult.FMessage = err.Message;
            }
            return GetPosBillResult(bOk, json, true);
        }

        /// <summary>
        /// 资料查询
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public JsonResult GetBaseInfo(PrPosDataRequest request)
        {
            bool bOk = false;
            string json = "";
            MemberResultModel mResult = new MemberResultModel();
            try
            {
                CloudPosService mDataSvr = new CloudPosService();
                ArrayList mdata = mDataSvr.GetBaseInfo(request, ref bOk);

                if (bOk)
                {
                    json = PluSoft.Utils.JSON.Encode(mdata);
                }
                else
                {
                    json = "没有查询到相关数据.";
                }
            }
            catch (Exception err)
            {
                mResult.FMessage = err.Message;
            }
            return GetPosBillResult(bOk, json, true);
        }


        /// <summary>
        /// 商品资料修改保存
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public JsonResult SetGoodsData(GoodsModel model, HttpPostedFileBase fImgUrlFile)
        {
            bool bOk = false;
            string json = "";
            string imgFileName = "";
            if (fImgUrlFile != null)
            {
                //var imgPath = ConfigurationManager.AppSettings["ImgSavePath"];
                var imgPath = ConfigurationManager.AppSettings["ImgSavePath"];

                string path = imgPath + CommonFun.ConvertToInt(model.FCorpID).ToString();;
                if (!Directory.Exists(path)) Directory.CreateDirectory(path);

                imgFileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".jpg"; // @"{}{fImgUrlFile.FileName}";
                fImgUrlFile.SaveAs(Path.Combine(path, imgFileName));
                string sFilePach = Path.Combine(path, imgFileName);

                CloudPosService ccoud = new CloudPosService();
                ccoud.SaveServiceLog(sFilePach, "ImgSavePath", 0, 225);
            }

            CountPosDataModel mResult = new CountPosDataModel();
            try
            {
                CloudPosService mDataSvr = new CloudPosService();
                ArrayList mdata = mDataSvr.SetGoodsInfo(model, imgFileName, ref bOk);

                if (bOk)
                {
                    json = PluSoft.Utils.JSON.Encode(mdata);
                }
                else
                {
                    json = "没有查询到相关数据.";
                }

            }
            catch (Exception err)
            {
                json = err.Message;
            }
            return GetPosBillResult(bOk, json, true);
        }

        /// <summary>
        /// 数据查询
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public JsonResult GetStockTotal(PrPosDataRequest request)
        {
            bool bOk = false;
            string json = "";
            MemberResultModel mResult = new MemberResultModel();
            try
            {
                CloudPosService mDataSvr = new CloudPosService();
                ArrayList mdata = mDataSvr.GetStockTotal(request, ref bOk);

                if (bOk)
                {
                    json = PluSoft.Utils.JSON.Encode(mdata);
                }
                else
                {
                    json = "没有查询到相关数据.";
                }

            }
            catch (Exception err)
            {
                mResult.FMessage = err.Message;
            }
            return GetPosBillResult(bOk, json, true);
        }

        /// <summary>
        /// 商品分类保存
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public JsonResult SetGoodsTypeInfo(PrPosDataRequest request)
        {
            bool bOk = false;
            string json = "";
            string msg = "";
            MemberResultModel mResult = new MemberResultModel();
            try
            {
                CloudPosService mDataSvr = new CloudPosService();
                ArrayList mdata = mDataSvr.SetGoodsTypeInfo(request, ref bOk, ref msg);

                if (bOk)
                {
                    json = PluSoft.Utils.JSON.Encode(mdata);
                }
                else
                {
                    json = msg;
                }

            }
            catch (Exception err)
            {
                mResult.FMessage = err.Message;
            }
            return GetPosBillResult(bOk, json, true);
        }

        /// <summary>
        /// 数据查询
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public JsonResult GetPosDataSum(PrPosDataRequest request)
        {
            bool bOk = false;                  
            MemberResultModel mResult = new MemberResultModel();
            try
            {
                int sPageIndex = 0;
                CloudPosService mDataSvr = new CloudPosService();
                ArrayList mdata = mDataSvr.GetPosDataSum(request, ref bOk, ref sPageIndex);
                return ResultService.GetReportResult(mdata);

            }
            catch (Exception err)
            {
                mResult.FMessage = err.Message;
            }
            return ResultService.GetReportResultErr();
        }

        /// <summary>
        /// 单据列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public JsonResult GetBillDataList(PrPosDataRequest request)
        {
            bool bOk = false;
            MemberResultModel mResult = new MemberResultModel();
            try
            {
                int sPageIndex = 0;
                CloudPosService mDataSvr = new CloudPosService();
                ArrayList mdata = mDataSvr.GetBillDataList(request, ref bOk, ref sPageIndex);
                return ResultService.GetReportResult(mdata);

            }
            catch (Exception err)
            {
                mResult.FMessage = err.Message;
            }
            return ResultService.GetReportResultErr();
        }

        /// <summary>
        /// 交班
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public JsonResult SetStockCheckEnd(PrPosDataRequest request)
        {
            bool bOk = false;
            string json = "";
            PosClassModel mResult = new PosClassModel();
            mResult.KInterID = 0;
            mResult.FBillNo = "";
            try
            {
                CloudPosService mDataSvr = new CloudPosService();
                Int64 mBillID = mDataSvr.SetStockCheckEnd(request, ref mResult);

                Hashtable result = new Hashtable();
                result["fbillid"] = mResult.KInterID;
                result["fbillno"] = mResult.FBillNo;
                json = PluSoft.Utils.JSON.Encode(result);

                if (mBillID > 0) bOk = true;
            }
            catch (Exception err)
            {
                //mResult.FMessage = err.Message;
            }
            return GetPosBillResult(bOk, json, true);
        }



    }
}
